package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.UserHolder;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.em.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.NearUserVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MessagesService {
    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate template;

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserLikeApi userLikeApi;


    //根据环信账号查用户信息
    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        User user = userApi.findByHuanxin(huanxinId);
        UserInfo userInfo = userInfoApi.findById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, vo); //copy同名同类型的属性
        if (userInfo.getAge() != null) {
            vo.setAge(Integer.toString(userInfo.getAge()));
        }
        return vo;
    }

    //添加好友
    public void contacts(Long frindId) {
        //添加到环信
        Boolean aBoolean = template.addContact(Constants.HX_USER_PREFIX + frindId,
                Constants.HX_USER_PREFIX + UserHolder.getUserId());
        if (!aBoolean) {//如果注册成功添加好友关系
            throw new BusinessException(ErrorResult.error());
        }
        friendApi.save(frindId, UserHolder.getUserId());
    }

    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        Long userId = UserHolder.getUserId();
        List<Long> ids = friendApi.findByFriendId(userId);
        PageResult pr = userInfoApi.findByUserInfo(page, pagesize, keyword, ids);
        return pr;
    }
  //删除好朋友
    public void deleteFriend(Long userId, Long likeUserId) {
        friendApi.deleteFriend(userId,likeUserId);
    }


    //查看谁喜欢了我
    //查两张表
    public PageResult findLovesMeUser(Integer page, Integer pagesize) {
        Long userId = UserHolder.getUserId();
        List<Comment> comments = commentApi.findByLoveMeComments(page, pagesize, userId, CommentType.LOVE);
        Map<Long, Comment> userId1 = CollUtil.fieldValueMap(comments, "userId");//评论喜欢我的表和id
        List<Long> list1 = CollUtil.getFieldValues(comments, "userId", Long.class);
       List<UserLike> userLikeList= userLikeApi.findUserLike(page, pagesize,userId);
        Map<Long, UserLike> userId2 = CollUtil.fieldValueMap(userLikeList, "userId");//喜欢表喜欢我的
        List<Long> list2 = CollUtil.getFieldValues(userLikeList, "userId", Long.class);
        List<NearUserVo> vos =new ArrayList<>();//最后需要返回的vo
        Map<Long, UserInfo> byIds1 = userInfoApi.findByIds(list1, null, null);
        Map<Long, UserInfo> byIds2 = userInfoApi.findByIds(list2, null, null);


        for (Long aLong : list1) {
            UserInfo userInfo = byIds1.get(aLong);
            NearUserVo nearUserVo=new NearUserVo();
            nearUserVo.setAvatar(userInfo.getAvatar());
            Comment comment = userId1.get(aLong);
            Long time = comment.getCreated();//毫秒值
            nearUserVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));
            nearUserVo.setUserId(userInfo.getId());
            nearUserVo.setNickname(userInfo.getNickname());
            vos.add(nearUserVo);
        }
        for (Long aLong : list2) {
            UserInfo userInfo = byIds2.get(aLong);
            NearUserVo nearUserVo=new NearUserVo();
            nearUserVo.setAvatar(userInfo.getAvatar());
            UserLike userLike = userId2.get(aLong);
            Long time = userLike.getCreated();
            nearUserVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));
            nearUserVo.setUserId(userInfo.getId());
            nearUserVo.setNickname(userInfo.getNickname());
            vos.add(nearUserVo);
        }
        return new PageResult(page,pagesize,0,vos);
    }

    public PageResult findLikesMeUser(Integer page, Integer pagesize,  CommentType commentType) {
        Long userId = UserHolder.getUserId();
        List<Comment> comments= commentApi.findByLoveMeComments(page, pagesize, userId, commentType);
        Map<Long, Comment> userId1 = CollUtil.fieldValueMap(comments, "userId");
        List<Long> list1 = CollUtil.getFieldValues(comments, "userId", Long.class);
        List<NearUserVo> vos =new ArrayList<>();//最后需要返回的vo
        Map<Long, UserInfo> byIds1 = userInfoApi.findByIds(list1, null, null);
        for (Long aLong : list1) {
            UserInfo userInfo = byIds1.get(aLong);
            NearUserVo nearUserVo=new NearUserVo();
            nearUserVo.setAvatar(userInfo.getAvatar());
            Comment comment = userId1.get(aLong);
            Long time = comment.getCreated();//毫秒值
            nearUserVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));
            nearUserVo.setUserId(userInfo.getId());
            nearUserVo.setNickname(userInfo.getNickname());
            vos.add(nearUserVo);
        }
        return new PageResult(page,pagesize,0,vos);
    }
}
